using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace GoldenAgeHelper.common
{
    public class GARegex : Regex
    {
        public static string GetString(string input, string from, string to)
        {
            string msg = "";
            from = FormatStr(from);
            to = FormatStr(to);
            string pattern = from + "(?<value>[\\s\\S]*?)" + to;
            Match m = Regex.Match(input, pattern);
            if (m.Success)
            {
                msg = m.Groups["value"].Value;
            }
            else
            {
                msg = "";
            }

            return msg;

        }

        public static int GetInt(string input, string from, string to)
        {
            //string str = GetString(input, from, to);
            //int i = 0;
            //int.TryParse(str, out i);
            //return i;

            string msg = "";
            from = FormatStr(from);
            to = FormatStr(to);
            string pattern = from + "(?<value>[\\d]*?)" + to;
            Match m = Regex.Match(input, pattern);
            if (m.Success)
            {
                msg = m.Groups["value"].Value;
            }
            else
            {
                msg = "";
            }
            int i;
            int.TryParse(msg, out i);
            return i;
        }

        public static string[] GetArrstr(string input, string from, string to)
        {
            string[] msg;
            from = FormatStr(from);
            to = FormatStr(to);
            string pattern = from + "(?<value>[\\s\\S]*?)" + to;
            MatchCollection m = Regex.Matches(input, pattern);
            msg = new string[m.Count];
            for (int i = 0; i < m.Count; i++)
            {
                Match mm = m[i];
                msg[i] = mm.Groups["value"].Value;
            }

            return msg;
        }


        public static GroupCollection GetString(string input, string pattern)
        {
            Match m = Regex.Match(input, pattern);
            return m.Groups;
        }


        private static string FormatStr(string str)
        {
            string arr = "()[]?.+";
            string temp = str;
            foreach (char chars in arr)
            {
                temp = temp.Replace(chars.ToString(), "\\" + chars.ToString());
            }

            temp = temp.Replace("<*>", "([\\s\\S]*?)");
            return temp;
        }

        public static string DelHtml(string input)
        {
            string pattern = "<(.|\n)+?>";
            return GARegex.Replace(input, pattern, "");
        }

        public static string DelReturn(string input)
        {
            return input.Replace("\r\n", "").Replace("\n", "");
        }
    }
}
